package com.birdboot.core;

import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;

import java.io.*;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * 该线程的任务是与指定客户端完成HTTP交互
 */
public class ClientHandler implements Runnable{
    private Socket socket;

    public  ClientHandler(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        // 1、解析请求
        try {
            HttpServletRequest request = new HttpServletRequest(socket);
            HttpServletResponse response = new HttpServletResponse(socket);

            // 2、处理请求
            DispatcherServlet.getInstance().service(request,response);

            // 3、发送响应
            response.response();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //遵循Http协议要求，一问一答后关闭连接，finally里面的代码每次运行后都要运行
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
